package org.mp3store.servlets;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class RegistrationServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        try {
            checkParameters(request);
        } catch (IllegalArgumentException ex) {
            response.sendRedirect("registration.jsp?err=" + ex.getMessage());
        }
    }

    protected void checkParameters(HttpServletRequest request) {
        if(request.getParameter("login").isEmpty()) {
            throw new IllegalArgumentException("need to set login.");
        }
        if(request.getParameter("password").isEmpty()) {
            throw new IllegalArgumentException("need to set password.");
        }
        if(request.getParameter("password2").isEmpty()) {
            throw new IllegalArgumentException("need to set password 2.");
        }
        if(!request.getParameter("password").equals(request.getParameter("password2"))) {
            throw new IllegalArgumentException("password must be equals.");
        }
    }
}
